﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using ReportDesignerServer.Common;
using ReportDesignerServer.DataModel;
using ReportDesignerServer.DataModels;
using ReportDesignerServer.Dto;
using ReportDesignerServer.Services;
using System.Security.Claims;

namespace ReportDesignerServer.Controllers
{

    /// <summary>
    /// UserController
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly IHttpContextAccessor _httpContextAccessor;

        private UserService userService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="httpContextAccessor"></param>
        public UserController(IHttpContextAccessor httpContextAccessor, UserService userService)
        {
            _httpContextAccessor = httpContextAccessor;
            this.userService = userService;
        }

        /// <summary>
        /// 获取当前登录人信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public Rt GetUserInfo()
        {
            // 获取用户ID
            var userIdClaim = _httpContextAccessor.HttpContext.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier);
            if (userIdClaim == null)
            {
                return Rt.Fail("无法获取token");
            }
            string userId = userIdClaim.Value;

            // 获取用户其他信息...
            var user = userService.GetUserInfo(userId);
            if (user != null)
            {
                return Rt.SuccessObj(user);
            }
            else
            {
                return Rt.Fail("无法获取用户");
            }
        }


        /// <summary>
        /// 获取当前登录人信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Rt GetMenuRoute()
        {
            var res = userService.GetMenuRoute();
            // 获取用户其他信息...
            
            if (res != null)
            {
                return Rt.SuccessObj(res);
            }
            else
            {
                return Rt.Fail("无法获取权限");
            }
        }

        /// <summary>
        /// 保存患者信息
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost]
        public Rt SaveUserInfo([FromBody] User user)
        {
            // 获取用户其他信息...
            return userService.SaveUserInfo(user); 
        }

        /// <summary>
        /// 获取人员列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Rt GetUserList()
        {
            // 获取用户其他信息...
            var res = userService.GetUserList();
            return Rt.SuccessObj(res);
        }

        /// <summary>
        /// 获取单个患者信息
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        [HttpGet]
        public Rt GetUserById(string userid)
        {
            long uid = 0;
            if (string.IsNullOrEmpty(userid) || !long.TryParse(userid,out uid))
            {
                return Rt.Fail($"传入的用户id错误！");
            }
            var res = userService.GetUserById(uid);
            if (res != null)
            return Rt.SuccessObj(res);
            else return Rt.Fail("未获取到患者信息！");
        }

        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        [HttpPost]
        public Rt ReSetUserPass([FromBody] User user)
        {
            string res = userService.ReSetUserPass(user);
            if (res != null)
                return Rt.SuccessObj(res);
            else return Rt.Fail("重置密码失败！");
        }

        //-------------------科室-----

        /// <summary>
        /// 获取科室
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Rt GetDeptList()
        {
            // 获取用户其他信息...
            var res = userService.GetDeptList();
            return Rt.SuccessObj(res);
        }

        /// <summary>
        /// 添加科室
        /// </summary>
        /// <param name="dept"></param>
        /// <returns></returns>
        [HttpPost]
        public Rt SaveDeptInfo([FromBody] Dept dept)
        {
            // 获取用户其他信息...
            return userService.SaveDeptInfo(dept);
        }

        //----------加载角色字典----

        /// <summary>
        /// 获取角色
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Rt GetRoleList()
        {
            // 获取用户其他信息...
            var res = userService.GetRoleList();
            return Rt.SuccessObj(res);
        }
    }
}
